package com.hzit.service.impl;

import com.fasterxml.jackson.databind.BeanProperty;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.entity.Classes;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;


import com.hzit.util.PageResult;
import com.hzit.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	//1. 查询所有学生
	@Override
	public List<Student> findAll() {
		// 1.1 查询所有学生
		List<Student> all = studentMapper.findAll();
		for (Student student : all) {
			Classes classes = classesMapper.findById(student.getCid());
			// 1.2 指定学生所在班级
			student.setCname(classes.getCname());
		}
		return all;
	}

	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(Integer page, Integer pageSize) {
		// 2.1 开始分页
		PageHelper.startPage(page,pageSize);
		// 2.2 查询所有学生
		List<Student> students = findAll();
		// 2.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		// 2.4 返回
		return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
	}

	// 3. 分页带条件查询
	@Override
	public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
		// 3.1 开始分页
		PageHelper.startPage(page,pageSize);
		// 3.2 查询所有学生
		List<Student> students = studentMapper.search(vo);
		// 3.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		// 3.4 为学生的班级名称赋值
		List<Student> result = studentPage.getResult();
		for (Student student : result) {
			Classes classes = classesMapper.findById(student.getCid());
			// 3.4.1 指定学生所在班级
			student.setCname(classes.getCname());
		}
		// 3.4 返回
		return new PageResult<>(studentPage.getTotal(),result);
	}
}
